package com.watson.aggregate;

import com.watson.Application;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: mytest
 * @description: my test
 * @author: zhangpeng348@jd.com
 * @date: 2019-01-02 11:05
 **/
public class MyTest {

    public static void main(String[] args) {

        MyTest myTest = new MyTest();
        String src = "123";
        System.out.println(src);
        myTest.returnStr(src);
        System.out.println(src);

    }

    public String returnStr(String src) {
        src = src + "abc";
        return src;
    }

    public static int getHr(String dt) {
        if (dt == null || dt.isEmpty()) {
            return 0;
        }
        String formatDt = new SimpleDateFormat("yyyy-MM-dd-HH-mm")
                .format(new Date(Long.valueOf(dt)));
        return Integer.valueOf(formatDt.substring(11, 13));
    }

    public static void formatTest() {
        String str=null;
        //$使用
        str=String.format("格式参数$的使用：%2$s, %1$d", 99 , "abc");
        System.out.println(str);
        //+使用
        System.out.printf("显示正负数的符号：%+d与%d%n", 99,-99);
        //补O使用
        System.out.printf("最牛的编号是：%03d%n", 7);
        //空格使用
        System.out.printf("Tab键的效果是：% 8d%n", 7);
        //.使用
        System.out.printf("整数分组的效果是：%,d%n", 9989997);
        //空格和小数点后面个数
        System.out.printf("一本书的价格是：% 50.5f元%n", 49.8);
    }


    public static void test() {

        String test = "\t862155030390347-742344e5ffcf-6c6edf07e99bf9ae\tbaidu\t1\t1\t1\t1920*1080\tAndroid\t7.0\t24\tXiaomi\tXiaomi\tMI 5\t\tzh_CN\t1.0.0\t862155030390347\tcom.jd.stat.analytics.android.demo\t1.0\tdemo.jd.stat.MainActivity_JDLMTATestViewController\t中国联通\t1546501182614\t\t120.1234\t30.1234\tdemo.jd.stat.MainActivity\tdemo.jd.stat.ManualTrackActivity\ttracktab\t17673\t\t24\t4\t0\tWIFI\t50\t\t{\"tk_page_name\":\"demo.jd.stat.MainActivity\",\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\",\"pgfl\":[\"demo.jd.stat.MainActivity\",\"demo.jd.stat.ManualTrackActivity\",\"demo.jd.stat.MainActivity\"],\"tk_page_title\":\"京东移动统计\",\"title\":\"京东登录\"}\t\t118.242.35.162, 10.191.48.45\t中国\t上海\t上海\t1546501188350\t\t\t3\t\t\t2019-01-03\n";
        String[] split = test.split("\t");
        for (String s : split) {
            System.out.println(s);
        }
    }

    public static void initDelay() {
        try {
            Date nowDate = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            String format = dateFormat.format(nowDate);
            String[] split = format.split("-");
            int newDay = Integer.parseInt(split[2]) + 1;
            StringBuilder nextDate = new StringBuilder();
            nextDate.append(split[0]).append("-")
                    .append(split[1]).append("-")
                    .append(newDay).append("-").append("00-00-00");
            Date newDate = null;
            newDate = dateFormat.parse(nextDate.toString());
            System.out.println(dateFormat.format(newDate));
            System.out.println(dateFormat.format(nowDate));
            long initDelay = newDate.getTime() - nowDate.getTime();
            System.out.println(initDelay / 1000 / 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public static boolean renameFile(File file, String newFilePath) {
        if (!file.exists()) {
            return false;
        }
        if (!file.renameTo(new File(newFilePath))) {
            System.out.println("error");
            return false;
        }
        return true;
    }

    public static void mapTest() {
//        Map<String, String> test = new HashMap<>();
//        Map<String, String> test = new LinkedMap();
//        Map<String, String> test = new TreeMap<>();
        Map<String, String> test = new ConcurrentHashMap<>();

        System.out.println("start test");
        System.gc();
        long start = Runtime.getRuntime().freeMemory();
        System.out.println("start generate data !");
        Set<String> strings = Application.genUid(0, 6000000);
        System.out.println("generate data complete!");
        int pos = 0;
        for (String string : strings) {
            test.put(string, string);
//            if ((pos % 10000) == 0) {
//                System.out.println("size of: " + MySizeOf.sizeOf(test));
//                System.gc();
//                long end = Runtime.getRuntime().freeMemory();
//                System.out.println((pos + 1) + " * 10000 个元素占用内存(B):" + (end - start));
//            }
//            pos++;
        }
        strings = null;
        System.out.println("put data complete!");
        System.gc();
        long end = Runtime.getRuntime().freeMemory();
        System.out.println("一个HashMap对象占内存:" + (end - start));
        return;
    }
}
